// component/tvideocall/tvideocall.js
import TRTC from '../tvideocall2/trtc-wx'
Component({
  options: {
    pureDataPattern: /^_/ // 指定所有 _ 开头的数据字段为纯数据字段
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    playerList: [], //别人的视频列表参数
    pusher: {}, //live-pusher参数
    _rtcConfig: {
      sdkAppID: '', // 必要参数 开通实时音视频服务创建应用后分配的 sdkAppID
      userID: '', // 必要参数 用户 ID 可以由您的帐号系统指定
      userSig: '', // 必要参数 身份签名，相当于登录密码的作用
    },
  },

  /**
   * 组件生命周期
   */
  lifetimes: {
    created: function () {
      //在组件实例刚刚被创建时执行
    },
    attached: function () {
      // 在组件实例进入页面节点树时执行，在这里执行初始化
    },
    ready: function () {
      //在组件在视图层布局完成后执行
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
      this.methods.exitRoom();
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 初始化方法，此时还未进入房间
     * @param {页面的实例，传this} pageInstance 
     * @param {rtc配置参数} rtcConfig 
     */
    init: function (pageInstance, rtcConfig) {
      this.TRTC = new TRTC(pageInstance);
      const pusherConfig = {
        beautyLevel: 0,
        mode: "RTC"
      };
      //创建推流实例对象
      const _pusher = this.TRTC.createPusher(pusherConfig);
      this.setData({
        _rtcConfig: rtcConfig,
        pusher: _pusher.pusherAttributes
      });
    },
    /**
     * 只是进入进入房间，如需开始推流，请在此之后调用startPushLocal
     */
    enterRoom: function (roomID) {
      const config = Object.assign(this.data._rtcConfig, {
        roomID: roomID,
        scene: 'rtc'
      });
      this.setData({
        pusher: this.TRTC.enterRoom(config),
      });
      this.methods.bindTRTCRoomEvent();
    },
    /**
     * 退出房间
     */
    exitRoom: function () {
      const result = this.TRTC.exitRoom()
      // 状态机重置，会返回更新后的pusher和playerList
      this.setData({
        pusher: result.pusher,
        playerList: result.playerList,
      })
    },
    /**
     * 开始推流，同时开启摄像头预览
     */
    startPushLocal: function () {
      this.TRTC.getPusherInstance().start(); // 开始推流（autoPush的模式下不需要）
    },
    /**
     * 停止推流，同时停止摄像头预览
     */
    stopPushLocal: function () {
      this.TRTC.getPusherInstance().stop();
    },
    /**
     * 开始摄像头预览画面
     */
    startPreviewLocal: function () {
      this.TRTC.getPusherInstance().startPreview();
    },
    /**
     * 停止摄像头预览画面
     */
    stopPreviewLocal: function () {
      this.TRTC.getPusherInstance().stopPreview();
    },


    /**
     * 
     * @param {true : 静音 false:开启声音} mute 
     */
    muteAudio: function (mute) {
      let _pusher = this.TRTC.setPusherAttributes({
        enableMic: !mute,
      });
      let player = this.TRTC.getPlayerList();
      for (let i = 0; i < player.length; i++) {
        player = this.TRTC.setPlayerAttributes(player[i].id, {
          muteAudio: mute
        });
      }
      this.setData({
        pusher: _pusher,
        playerList: player
      });
    },
    /**
     * 绑定TRTC的回调事件
     */
    bindTRTCRoomEvent: function () {
      this.TRTC.on(TRTC_EVENT.LOCAL_JOIN, (event) => {
        this.triggerEvent("onLocalJoin", {
          event: event
        })
      });
      // 收到视频流新增的通知
      this.TRTC.on(EVENT.REMOTE_VIDEO_ADD, (event) => {
        const {
          player
        } = event.data
        // 开始播放远端的视频流，默认是不播放的
        let playerList = this.TRTC.setPlayerAttributes(player.id, {
          'muteVideo': false
        })
        // 如果您是1V3的教育场景，学生端这里可能会需要处理下playerList的顺序，如将教师的那个player放置于首位。
        this.setData({
          playerList: playerList,
        })
      });

      //远端用户加入房间
      this.TRTC.on(REMOTE_USER_JOIN, (event) => {
        this.triggerEvent("onUserJoin", {
          event: event
        })
      });
      // 远端用户退出
      this.TRTC.on(TRTC_EVENT.REMOTE_USER_LEAVE, (event) => {
        const {
          playerList
        } = event.data
        this.setData({
          playerList: playerList
        })
        this.triggerEvent("onUserLeave", {
          event: event
        })
      });
    },


    /**
     * ----------------------pusher和player的事件监听，他们会被映射成TRTC类里on内的监听-----------------
     */

    // 请保持跟 wxml 中绑定的事件名称一致
    _pusherStateChangeHandler(event) {
      this.TRTC.pusherEventHandler(event)
    },
    _pusherNetStatusHandler(event) {
      this.TRTC.pusherNetStatusHandler(event)
    },
    _pusherErrorHandler(event) {
      this.TRTC.pusherErrorHandler(event)
    },
    _pusherBGMStartHandler(event) {
      this.TRTC.pusherBGMStartHandler(event)
    },
    _pusherBGMProgressHandler(event) {
      this.TRTC.pusherBGMProgressHandler(event)
    },
    _pusherBGMCompleteHandler(event) {
      this.TRTC.pusherBGMCompleteHandler(event)
    },
    _pusherAudioVolumeNotify(event) {
      this.TRTC.pusherAudioVolumeNotify(event)
    },
    _playerStateChange(event) {
      this.TRTC.playerEventHandler(event)
    },
    _playerFullscreenChange(event) {
      this.TRTC.playerFullscreenChange(event)
    },
    _playerNetStatus(event) {
      this.TRTC.playerNetStatus(event)
    },
    _playerAudioVolumeNotify(event) {
      this.TRTC.playerAudioVolumeNotify(event)
    },
  }
})
